using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class DragMove : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler
{
    Vector3 offset;
    Vector3 defPos;

    private void Start()
    {
        GameManager.Instance.inArea.Add(gameObject);
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        defPos = transform.position;
        offset = transform.position - Input.mousePosition;
        GetComponent<CanvasGroup>().blocksRaycasts = false;
    }

    public void OnDrag(PointerEventData eventData)
    {
        if (eventData.pointerCurrentRaycast.gameObject == null) return;
            if (eventData.pointerCurrentRaycast.gameObject.tag == Consts.T_PlaceArea) {
            transform.position = Input.mousePosition+offset;
        }
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        GetComponent<CanvasGroup>().blocksRaycasts = true;
        if (eventData.pointerCurrentRaycast.gameObject != null) {
            if (eventData.pointerCurrentRaycast.gameObject.tag == Consts.T_PlaceArea)
            {
                transform.position = Input.mousePosition + offset;
                return;
            }
        }
        transform.position = defPos;
    }
}
